#!/bin/bash
#
# USAGE:
# 	superMasterResample
#
# DESCRIPTION:
#  superMasterResample is an ADORE function.  
#
# INPUT:
#
# OUTPUT:
#
# EXAMPLES:
#  superMasterResample


function processResampleSteps(){
   # reload original settings
   settings load ${originalSettingsFile}
   master=${resampleMaster}
   local scene=${1}
   if [ -d "${resampleFolder}/${master}_${scene}" ]; then
     continue #already processed... Shouldn't happen but who knows.
   fi
   if [ "${master}" == "${scene}" ]; then
     #re-crop master
     bold "Processing resample/${master}_${scene}"
     settings apply -r -q master=${master} slave=dummy outputFolder="${resampleFolder}/${master}_${scene}" m_dbow="${rs_dbow}" m_dbow_geo="${rs_dbow_geo}"
     mkdir -p "${outputFolder}"
     cd "${outputFolder}"
     local cropSteps=`previousSteps ${cropsFolder}/${master}/${master}.res`
     if [[ "${cropSteps}" == *m_fbd2fbs* ]]; then
       [ "${m_dbow}" == "0 0 0 0" ] && echo "Can't use rs_dbow_geo for Master with m_fbd2fbs" > README.txt && continue
       #copy the master.res and manually crop the file.
       cp ${cropsFolder}/${master}/${master}.res ${outputFolder}
       call "(filename resfile format numpixels numlines)=dorisProcess2OutputFile m_crop"
       local dbow_arr=( ${m_dbow} )
       local cropFile=`basename ${filename}.crop` 
       echo "cpxfiddle -f ${format} -w ${numpixels} -q normal -o float -l ${dbow_arr[0]} -L ${dbow_arr[1]} -p ${dbow_arr[2]} -P ${dbow_arr[3]} ${filename} > ${outputFolder}/${cropFile}"
       cpxfiddle -f ${format} -w ${numpixels} -q normal -o float -l ${dbow_arr[0]} -L ${dbow_arr[1]} -p ${dbow_arr[2]} -P ${dbow_arr[3]} ${filename} > ${outputFolder}/${cropFile}
       modifyRes.sh ${outputFolder}/${master}.res crop "Data_output_file" ${outputFolder}/${cropFile}
       modifyRes.sh ${outputFolder}/${master}.res crop "First_line (w.r.t. ovs_image)" ${dbow_arr[0]}
       modifyRes.sh ${outputFolder}/${master}.res crop "Last_line (w.r.t. ovs_image)" ${dbow_arr[1]}
       modifyRes.sh ${outputFolder}/${master}.res crop "First_pixel (w.r.t. ovs_image)" ${dbow_arr[2]}
       modifyRes.sh ${outputFolder}/${master}.res crop "Last_pixel (w.r.t. ovs_image)" ${dbow_arr[3]}
       modifyRes.sh ${outputFolder}/${master}.res crop "Number of lines (oversampled)" $(( ${dbow_arr[1]} - ${dbow_arr[0]} + 1 ))
       modifyRes.sh ${outputFolder}/${master}.res crop "Number of pixels (oversampled)" $(( ${dbow_arr[3]} - ${dbow_arr[2]} + 1 ))
     else     
         if [ "${parallel}" == "on" ]; then
           jobId=`quejob "${cropSteps}" "${outputFolder}"`
           jobIds=( ${jobIds[@]} ${jobId} )
         else
           eval "${cropSteps}"
         fi
     fi
   else
     bold "Processing resample/${master}_${scene}"
     settings apply -r -q master=${master} slave=${scene} outputFolder="${resampleFolder}/${master}_${scene}"
     mkdir -p "${outputFolder}"
     cd "${outputFolder}"
     cp ${cropsFolder}/${master}/${master}.res ${outputFolder}/${master}.res
     cp ${cropsFolder}/${slave}/${slave}.res ${outputFolder}/${slave}.res
     
     if [ "${parallel}" == "on" ]; then
       jobId=`quejob "${resampleSteps}" "${outputFolder}"`
       jobIds=( ${jobIds[@]} ${jobId} )
     else
       eval "${resampleSteps}"
     fi
   fi
   settings save
}
function processResample(){
  local resampleSteps
  if [ -f "${sam_in_dem}" ]; then 
    resampleSteps='settings apply -r -q int_multilook="1 1" srp_multilook="1 1" crd_include_fe=on;coarseorb;coarsecorr;fine;coregpm;resample;interfero;comprefdem;addrefpha2s_crop'
  else
    resampleSteps='settings apply -r -q int_multilook="1 1" srp_multilook="1 1" srp_dumprefpha=on;coarseorb;coarsecorr;fine;coregpm;resample;interfero;comprefpha;subtrrefpha;addrefpha2s_crop'
  fi
  # we will be changing directories
  # save the current dir.
  local CALLDIR="$PWD";

  generateRandomString
  local originalSettingsFile="${CALLDIR}/${randomString}.set"
  #save current settings so that we can recall them at the end.
  settings save ${originalSettingsFile}
  ####Now resample
  cd ${CALLDIR} #if pairs file is given by relative path we want to be in CALLDIR.
  jobIds=( ); #start empty list
  if [ -z "${singleSlave}" ]; then
    for scene in `scenes include| tr " " "\n" | sort --unique`
    do
      processResampleSteps "${scene}"
    done
  else
     processResampleSteps "${singleSlave}"
  fi

  #return back
  cd ${CALLDIR}
  settings load ${originalSettingsFile}
  #cleanup
  rm -rf ${originalSettingsFile}
}

##MAIN
resampleFolder="${runFolder}/resample"
[ -n "${1}" ] && resampleMaster="${1}"
[ -n "${2}" ] && singleSlave="${2}" && echo "Will resample a single image: ${singleSlave}"
[ -z "${resampleMaster}" ] && error "Please specify the super resampling master. See usage: ? superMasterResample" && return
[ ! -d "${cropsFolder}/${resampleMaster}" ] && error "Bad super master. ${cropsFolder}/${resampleMaster} does not exist." && return 
[ "${rs_dbow}" == "0 0 0 0" ] && [ "${rs_dbow_geo}" == "0 0 0 0" ] && error "Please specify rs_dbow or rs_dbow_geo." && return
processResample

